<?php

namespace app\models;

use app\components\Calculation;
use Yii;

/**
 * This is the model class for table "feed".
 *
 * @property int $fid 主键id
 * @property int $pid 宠物id
 * @property int $user_id 用户id
 * @property string $amount 喂食数量
 * @property string $height 增加体重
 * @property string $grow 增加成长值
 * @property string $nuggets 增加掘金
 * @property string $to_date 日期
 * @property int $created_at 创建时间
 * @property int $update_at 更新时间
 */
class Feed extends \yii\db\ActiveRecord
{
    const MAX_FEED = 5;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'feed';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'user_id', 'created_at', 'update_at'], 'integer'],
            [['amount', 'height', 'grow', 'nuggets'], 'number'],
            [['to_date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'fid' => '主键id',
            'pid' => '宠物id',
            'user_id' => '用户id',
            'amount' => '喂食数量',
            'height' => '增加体重',
            'grow' => '增加成长值',
            'nuggets' => '增加掘金',
            'to_date' => '日期',
            'created_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    /**
     * 获取当天的喂食总金额
     * @param $userId
     * @param $pid
     * @return bool|mixed
     */
    public function getTodaySumFeed($pid)
    {
        if (empty($pid)) {
            return false;
        }
        $toDate = date('Y-m-d');
        return $this->find()->where('pid=:pid and to_date=:to_date', [
            ':pid' => $pid,
            ':to_date' => $toDate
        ])->sum('amount');
    }

    /**
     * @param $userId
     * @param $pid
     * @param $amount
     * @return array
     */
    public function addFeed($userId, $pid, $amount)
    {
        $toDate = date('Y-m-d');
        $petRow = (new Pet())->find()->where('pid=:pid', [':pid' => $pid])->one();
        $grow = $petRow->grow;
        $multiple = Calculation::get_rand(['0.5'=>35,'1'=>50, '2' => 15]);
        $height = $multiple * $grow;
        if($petRow->grow == $petRow->nuggets) {//如果双属性
            $grow = $nuggets = Calculation::get_rand(['0' => 70,'0.05'=>20, '0.1' => 10]);
        } else {
            $grow = Calculation::get_rand(['0' => 70,'0.05'=>20, '0.1' => 10]);
            $nuggets = Calculation::get_rand(['0' => 70,'0.05'=>20, '0.1' => 10]);
        }
        $User = new User();
        $User->reduceBlance($userId, 10);//买家扣除余额
        $this->grow = $grow;
        $this->height = $height;
        $this->user_id = $userId;
        $this->nuggets = $nuggets;
        $this->pid = $pid;
        $this->amount = $amount;
        $this->to_date = $toDate;
        $this->created_at = $this->update_at = time();
        $this->save();
        $petRow->height = $petRow->height + $height;
        $petRow->nuggets = $petRow->nuggets + $nuggets;
        $petRow->grow = $petRow->grow + $grow;
        $petRow->save();
        return [
            'height' => $height,
            'user_id' => $userId,
            'nuggets' => $nuggets,
            'grow' => $grow
        ];
    }

    /**
     * 获取宠物历史喂养记录
     * @param $userId
     * @param $pid
     * @param int $maxLen
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function getHistoryFeed($userId, $pid, $maxLen = 0)
    {
        if (empty($userId) || empty($pid)) {
            return false;
        }
        $query = $this->find()->where('user_id=:user_id and pid=:pid', [':user_id' => $userId, ':pid' => $pid])
            ->orderBy('created_at', 'desc');
        if ($maxLen != 0) {
            return $query->limit($maxLen)->all();
        }
        return $query->all();
    }
}
